package configs

import "github.com/spf13/viper"

type GlobalConfig struct {
	Server struct {
		Addr       string `yaml:"addr" json:"addr" cfg:"addr"`
		Debug      bool   `yaml:"debug" json:"debug" cfg:"debug"`
		PProfToken string `yaml:"pprofToken" json:"pprofToken" cfg:"pprofToken"`
		LogPath    string `yaml:"logPath" json:"logPath" cfg:"logPath"`
	} `yaml:"server" json:"server" cfg:"server"`
	Mysql struct {
		MaxOpenConns int    `yaml:"maxopenconns" json:"maxopenconns" cfg:"maxopenconns"`
		MaxIdleConns int    `yaml:"maxidleconns" json:"maxidleconns" cfg:"maxidleconns"`
		MaxLifeTime  int    `yaml:"maxlifetime" json:"maxlifetime" cfg:"maxlifetime"`
		Username     string `yaml:"username" json:"username" cfg:"username"`
		Password     string `yaml:"password" json:"password" cfg:"password"`
		Host         string `yaml:"host" json:"host" cfg:"host"`
		DB           string `yaml:"db" json:"db" cfg:"db"`
	} `yaml:"mysql" json:"mysql" cfg:"mysql"`
	Redis struct {
		Password     string `yaml:"password" json:"password" cfg:"password"`
		URL          string `yaml:"url" json:"url" cfg:"url"`
		DB           int    `yaml:"db" json:"db" cfg:"db"`
		MaxRetries   int    `yaml:"maxretries" json:"maxretries" cfg:"maxretries"`
		PoolSize     int    `yaml:"poolsize" json:"poolsize" cfg:"poolsize"`
		MinIdleConns int    `yaml:"minidleconns" json:"minidleconns" cfg:"minidleconns"`
	} `yaml:"redis" json:"redis" cfg:"redis"`
	JWT struct {
		Secret     string `json:"secret" yaml:"secret" cfg:"secret"`
		Expire     int    `json:"expire" yaml:"expire"`
		MaxRefresh int    `json:"maxrefresh" yaml:"maxrefresh"`
	} `yaml:"jwt" json:"jwt" cfg:"jwt"`
	CORS struct {
		AllowOrigins     string `json:"alloworigins" yaml:"alloworigins"`
		AllowMethods     string `json:"allowmethods" yaml:"allowmethods"`
		AllowHeaders     string `json:"allowheaders" yaml:"allowheaders"`
		ExposeHeaders    string `json:"exposeheaders" yaml:"exposeheaders"`
		AllowCredentials bool   `json:"allowcredentials" yaml:"allowcredentials"`
		MaxAge           int    `json:"maxage" yaml:"maxage"`
	} `yaml:"cors" json:"cors"`
	RateLimit struct {
		Max    float64 `json:"max" yaml:"max"`
		Expire int64   `json:"expire" yaml:"expire"`
	} `yaml:"ratelimit" json:"ratelimit"`
	Secure struct {
		STSSeconds              int64  `json:"stsseconds" yaml:"stsseconds"`
		STSIncludeSubdomains    bool   `json:"stsincludesubdomains" yaml:"stsincludesubdomains"`
		FrameDeny               bool   `json:"framedeny" yaml:"framedeny"`
		ContentTypeNosniff      bool   `json:"contenttypenosniff" yaml:"contenttypenosniff"`
		BrowserXssFilter        bool   `json:"browserxssfilter" yaml:"browserxssfilter"`
		IENoOpen                bool   `json:"ienoopen" yaml:"ienoopen"`
		SSLProxyHeaders         string `json:"sslproxyheaders" yaml:"sslproxyheaders"`
		CustomFrameOptionsValue string `json:"customframeoptionsvalue" yaml:"customframeoptionsvalue"`
		AllowHosts              string `yaml:"allowhosts" json:"allowhosts" cfg:"allowhosts"`
		SSLRedirect             bool   `json:"sslredirect" yaml:"sslredirect"`
		ContentSecurityPolicy   string `json:"contentsecuritypolicy" yaml:"contentsecuritypolicy"`
		ReferrerPolicy          string `json:"referrerpolicy" yaml:"referrerpolicy"`
	} `yaml:"secure" json:"secure"`
}

var Config *GlobalConfig

func InitConfig(path string) {
	viper.SetConfigName("config")
	viper.AddConfigPath(path)
	err := viper.ReadInConfig()
	if err != nil {
		panic(err)
	}
	err = viper.Unmarshal(&Config)
	if err != nil {
		panic(err)
	}
}
